package com.bsoft.gol.hcs.clinical.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import pcn.consultation.MedicalRecordTypeDept;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class MedicalRecordTypeDeptDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<MedicalRecordTypeDept> {
    public MedicalRecordTypeDeptDAO() {
        this.setKeyField("medicalRecordTypeId");
    }

    @DAOMethod(sql = "delete from MedicalRecordTypeDept where medicalRecordTypeId=:medicalRecordTypeId")
    public abstract void deleteDeptById(@DAOParam("medicalRecordTypeId") String medicalRecordTypeId);

    public List<String> queryBaseMedicalRecordTypeDeptById(final String id) {
        HibernateStatelessResultAction<List<String>> action = new AbstractHibernateStatelessResultAction<List<String>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("select deptId from  base_medicalRecordType_dept where  medicalRecordTypeId = '"+id+"' ");
                Query query = ss.createSQLQuery(sql.toString());
                query = (SQLQuery) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = query.list();

                List<String> deptIdList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(dbList)) {
                    for (Map map : dbList) {
                        deptIdList.add((String) map.get("deptId"));
                    }
                    setResult(deptIdList);
                } else {
                    setResult(null);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        List<String> deptIdList = action.getResult();
        return deptIdList;
    }


    public List<String> queryBaseMedicalRecordTypeDeptNameById(final String id) {
        HibernateStatelessResultAction<List<String>> action = new AbstractHibernateStatelessResultAction<List<String>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer sql = new StringBuffer();
                sql.append("select b.title from  base_medicalRecordType_dept a,app_department b where a.deptId = b.id and a.medicalRecordTypeId = '"+id+"' ");
                Query query = ss.createSQLQuery(sql.toString());
                query = (SQLQuery) query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = query.list();

                List<String> deptIdList = new ArrayList<>();
                if (CommonUtils.isNotEmpty(dbList)) {
                    for (Map map : dbList) {
                        deptIdList.add((String) map.get("name"));
                    }
                    setResult(deptIdList);
                } else {
                    setResult(null);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        List<String> deptNameList = action.getResult();
        return deptNameList;
    }


}
